Skip to content

fix: resolve props defined only via React.FC<Props> / React component type annotations (fixes #387) - #1208

Open
DeviousCardi wants to merge 1 commit into
reactjs:8.xfrom
DeviousCardi:fix/387-fc-interface-props
Open

DeviousCardi wants to merge 1 commit into
reactjs:8.xfrom
DeviousCardi:fix/387-fc-interface-props

Conversation

@DeviousCardi

Copy link
Copy Markdown

Fixes #387

Problem

When a component's props are only defined through a React function component type annotation — e.g.

interface Props { foo: string; bar?: number }
const MyComponent: React.FC<Props> = () => <div />;

— react-docgen extracted no props at all whenever the source was parsed with the Flow parser (the GenericTypeAnnotation shape was not recognized), and also missed props for React.memo<Props>(...) and for named functions assigned to annotated variables (const C: React.FC<Props> = Inner).

Root cause

findAssignedVariableType in src/utils/getTypeFromReactComponent.ts only handled TSTypeReference annotations with four hard-coded names, so:

  • Flow-parsed React.FC<Props> (GenericTypeAnnotation) → ignored
  • React.SFC / React.StatelessComponent → ignored
  • React.memo<Props>(fn) generics → never consulted
  • const C: React.FC<Props> = Inner (component resolved to the referenced function) → no parent declarator → ignored

Fix

  • getFunctionComponentPropsType() recognizes FunctionComponent, FC, VoidFunctionComponent, VFC (plus deprecated SFC/StatelessComponent) in both TS (TSTypeReference) and Flow (GenericTypeAnnotation) annotations.
  • findReferencedVariableType() scans for const C: React.FC<Props> = <this component> when the inline declarator lookup fails.
  • getMemoPropsType() reads the props from the generic type argument of a wrapping React.memo<Props>(...) call (TS and Flow).
  • unwrapBuiltinTSPropTypes now also unwraps Flow React.PropsWithChildren / PropsWithRef / PropsWithoutRef wrappers.

Tests

  • Unit tests in getTypeFromReactComponent-test.ts (TS + Flow FC annotations, aliases, memo generics, assigned function reference, PropsWithChildren unwrap) with snapshots.
  • Handler tests in codeTypeHandler-test.ts (props extraction from interfaces, incl. extends heritage, for flow + ts + memo + assigned-reference).
  • Integration fixtures component_44.tsx (FC + interface extends), component_45.tsx (memo generics), component_46.tsx (assigned named function), component_47.js (flow FC + interface) with snapshots.

Verification

  • npx vitest run in packages/react-docgen: 69 files / 1513 tests passed (was 1494 before; +19 new).
  • packages/react-docgen-cli: 57 tests passed (after tsc build).
  • npx tsc --noEmit clean; eslint (prettier) clean.
  • Diff: 696 insertions / 22 deletions across 11 files.

Written with assistance from Mistral Vibe (AI), reviewed and tested by me.

…omponent type annotations (fixes reactjs#387)

Components whose props are only expressed through a React function
component type annotation (e.g. `interface Props {...}` referenced by
`const C: React.FC<Props> = () => ...`) now get their props extracted.

Root cause: findAssignedVariableType only recognized TypeScript
TSTypeReference annotations, so Flow-parsed sources (GenericTypeAnnotation)
and several component shapes yielded no props at all.

Changes:
- getTypeFromReactComponent: recognize React.FC/FunctionComponent/VFC/
  VoidFunctionComponent (plus deprecated SFC/StatelessComponent) in both
  TS (TSTypeReference) and Flow (GenericTypeAnnotation) annotations
- resolve props when a named function is assigned to an annotated variable
  (`const C: React.FC<Props> = Inner`)
- resolve props from the generic argument of `React.memo<Props>(...)`
- unwrapBuiltinTSPropTypes: also unwrap Flow GenericTypeAnnotation wrappers
  (React.PropsWithChildren/PropsWithRef/PropsWithoutRef)

Regression tests: unit tests in getTypeFromReactComponent-test and
codeTypeHandler-test, plus integration fixtures component_44-47.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>

Signed-off-by: DeviousCardi <115358213+DeviousCardi@users.noreply.github.com>
@changeset-bot

changeset-bot Bot commented Nov 21, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 88ebd83

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
react-docgen Patch
@react-docgen/cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Typescript support for defining component Interface through React interface

1 participant